package com.cenjiawen.common;

import com.fenda.common.model._MappingKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.zhenyi.fenda.controller.ApiController;
import com.zhenyi.fenda.core.FDRoutes;
import com.zhenyi.fenda.core.ws.WSServer;

import cn.hutool.core.lang.Singleton;

public class FendaConfig extends Config {

	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
		JFinal.start("ROOT", 9090, "/");
		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
	}

	@Override
	public void configRoute(Routes me) {
		super.configRoute(me);
		me.add(new FDRoutes());
		me.add("/", ApiController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		super.configPlugin(me);
		// java.util.List<IPlugin> list = me.getPluginList();
		// for (IPlugin iPlugin : list) {
		// if (iPlugin instanceof ActiveRecordPlugin) {
		// // ((ActiveRecordPlugin) iPlugin).addSqlTemplate("ty_user.sql");
		// // ((ActiveRecordPlugin)
		// // iPlugin).addSqlTemplate("ty_admin.sql");
		// // ((ActiveRecordPlugin) iPlugin).addSqlTemplate("ytweb.sql");
		// ((ActiveRecordPlugin)
		// iPlugin).setBaseSqlTemplatePath(PathKit.getRootClassPath());
		// ((ActiveRecordPlugin) iPlugin).addSqlTemplate("fenda.sql");
		// // ((ActiveRecordPlugin) iPlugin).addSqlTemplate("toupiao.sql");
		// }
		// }
	}

	@Override
	protected void configSQLConnection(Plugins me) {
		// super.configSQLConnection(me);
		addDataSource("test.zhen-yee.com", "fenda", "root", "zhenyi", me, new ArpHandler() {

			@Override
			public ActiveRecordPlugin configMapping(ActiveRecordPlugin arg0) {
				_MappingKit.mapping(arg0);
				arg0.setBaseSqlTemplatePath(PathKit.getRootClassPath());
				arg0.addSqlTemplate("fenda.sql");
				return arg0;
			}
		});
	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();

		try {
			WSServer wsServer = Singleton.get(WSServer.class);
			wsServer.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		ApiConfig pxlmCfg = new ApiConfig("token", "wx24524e8b092948cf", "a37ea37c395dd493257257217b95c2fa");
		pxlmCfg.setEncodingAesKey("");
		pxlmCfg.setEncryptMessage(true);
		ApiConfigKit.putApiConfig(pxlmCfg);

		String appId = PropKit.get("appId");
		if (StrKit.isBlank(appId)) {
			return;
		}
		String token = PropKit.get("token");
		String appSecret = PropKit.get("appSecret");
		String encodingAesKey = PropKit.get("encodingAesKey");
		ApiConfig config = new ApiConfig(token, appId, appSecret);
		config.setEncodingAesKey(encodingAesKey);
		config.setEncryptMessage(true);
		ApiConfigKit.putApiConfig(config);
		ApiConfigKit.setThreadLocalAppId(appId);
		
	}

	@Override
	public void beforeJFinalStop() {
		super.beforeJFinalStop();
		try {
			WSServer wsServer = Singleton.get(WSServer.class);
			wsServer.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
